using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Xml.XPath;

namespace CriticalSystems.Instrumentation
{
    [RunInstaller(true)]
    public partial class EventLogInstaller : Installer
    {
        private readonly System.Diagnostics.EventLogInstaller eventLogInstaller;

        public EventLogInstaller()
        {
            InitializeComponent();
            // Create an instance of an EventLogInstaller.
            this.eventLogInstaller = new System.Diagnostics.EventLogInstaller();

            Stream xmlStream = ResourceUtilities.GetEmbeddedResource(GetType(), "SystemInformation.xml");
            IXPathNavigable xml = new XPathDocument(xmlStream);
            XPathNavigator navigator = xml.CreateNavigator();
            navigator.MoveToFirstChild();
            string system = navigator.GetAttribute("name", string.Empty);

            // Set the source name of the event log.
            this.eventLogInstaller.Source = system;

            // Set the event log that the source writes entries to.
            string eventLog = navigator.GetAttribute("eventLog", string.Empty);
            if (!string.IsNullOrEmpty(eventLog))
            {
                this.eventLogInstaller.Log = eventLog;
            }

            this.eventLogInstaller.CategoryResourceFile = GetType().Assembly.Location;
            this.eventLogInstaller.CategoryCount = navigator.SelectChildren(XPathNodeType.Element).Count;
            this.eventLogInstaller.MessageResourceFile = GetType().Assembly.Location;

            // Add myEventLogInstaller to the Installer collection.
            Installers.Add(this.eventLogInstaller); 
        }
    }
}